package com.s2paganini.data 
{
	import flash.text.StyleSheet;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * @author silvio paganini | s2paganini.com
	 */
	public class CSSLoader extends URLLoader
	{
		private static var instance : CSSLoader;
		private var cssLoaded : StyleSheet;
		public static const CSS_LOADED : String = "cssLoaded";
		
		public static function getInstance() : CSSLoader{
			return instance ? instance : instance = new CSSLoader();
		}
		
		function CSSLoader(){
			addEventListener(Event.COMPLETE, storeCSS);
			addEventListener(IOErrorEvent.IO_ERROR, listenerError);
			addEventListener(IOErrorEvent.NETWORK_ERROR, listenerError);
		}
		
		private function listenerError(event : IOErrorEvent) : void
		{
		}

		public function loadCSS(url : String):void{
			load(new URLRequest(url));
		}
		
		private function storeCSS(event : Event) : void
		{
			cssLoaded = new StyleSheet();
			cssLoaded.parseCSS(event.target.data);
			dispatchEvent(new Event(CSSLoader.CSS_LOADED));
		}
		
		public function get _cssLoaded() : StyleSheet {
			return cssLoaded;
		}
	}
}
